<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Widgets for displaying a gallery of photos related to the photo set.
 * 
 * MUST includes the libaray 'ad-gallery'.
 * ADD the css and js file at the action where would use the widget
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.1
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_widget.php';

// Definitions.
define('GALLERY_MAX_ITEM_COUNT',		100);

/**
 * Widget for displaying a grid of photos.
 */
class PhotosetGallery extends Widget
{
	/**
	 * Item object that constructed the gallery content.
	 */
	var $obj = null;
	
	/**
	 * Constructor.
	 */
	function __construct(&$obj, $out = null)
	{
		parent::__construct($out);
		$this->obj =& $obj;
	}
	
	/**
	 * Starts a gallery.
	 * The notice or some sayings of the gallery. Might some labels.
	 * 
	 * Might be overrided.!
	 */
	function showStart()
	{
		// The gallery code.
		$this->out->elementStart('div', array('id' => 'ad-container',
											  'class' => 'ad-container'));
		$this->out->elementStart('div', array('id' => 'gallery',
										 	  'class' => 'ad-gallery'));
		
		$this->out->element('div', array('class' => 'ad-image-wrapper'));
		$this->out->element('div', array('class' => 'ad-controls'));

		$this->out->elementStart('div', array('class' => 'ad-nav'));
		$this->out->elementStart('div', array('class' => 'ad-thumbs'));
		$this->out->elementStart('ul', array('class' => 'ad-thumb-list'));
	}
	
	/**
	 * Ends a gallery.
	 */
	function showEnd()
	{
		$this->out->elementEnd('ul');
		$this->out->elementEnd('div');
		$this->out->elementEnd('div');
		$this->out->elementEnd('div');
		$this->out->elementEnd('div');
	}
	
	/**
	 * Show the grid
	 * 
	 * @return int, count of item listed.
	 */
	function show()
	{
		$this->showStart();

		$itemcnt = 0;
		while ($this->obj->fetch() && $itemcnt <= GALLERY_MAX_ITEM_COUNT) {
			++$itemcnt;
			if ($itemcnt > GALLERY_MAX_ITEM_COUNT) {
				break;
			}
			
			try {
				$item = $this->newGalleryItem($this->obj);
				$item->show();
			} catch (Exception $e) {
				// FIXME: Log?
				continue;
			}
		}
		
		$this->showEnd();
		
		return $itemcnt;
	}
	
	/**
	 * Returns a new grid item for the current object.
	 */
	function newGalleryItem(&$obj)
	{
		return new PhotosetGalleryItem($obj, $this->out);
	}
}

/**
 * Widget for representing a single photo item.
 */
class PhotosetGalleryItem extends Widget
{
	/**
	 * The item this item will show().
	 */
	var $obj = null;
	
	/**
	 * Thumbnail url.
	 */
	var $thumbnailurl;
	
	/**
	 * Original url.
	 */
	var $originalurl;
	
	/**
	 * Constructor.
	 */
	function __construct($obj, $out = null)
	{
		parent::__construct($out);
		
		$this->obj =& $obj;
	}
	
	/**
	 * Recipe function for displaying a single item.
	 */
	function show()
	{
		if (empty($this->obj)) {
			return;
		}
		
		$this->prepareData();
		$this->showStart();
		$this->showPhotoInfo();
		$this->showEnd();
	}
	
	/**
	 * Do some data preparation.
	 */
	function prepareData()
	{
		$this->thumbnailurl = $this->obj->thumbnailurl();
		$this->originalurl = $this->obj->photourl();
	}
	
	/**
	 * Start a single item.
	 * Overriding.
	 */
	function showStart()
	{
		$this->out->elementStart('li');
	}
	
	/**
	 * Finish the note news item.
	 */
	function showEnd()
	{
		$this->out->elementEnd('li');
	}
	
	/**
	 * Overriding.
	 */
	function showPhotoInfo()
	{
		$this->out->elementStart('a', array('href' => $this->originalurl));
		$this->out->element('img', array('src' => $this->thumbnailurl,
									'title' => '',
									'alt' => '',
									'longdesc' => $this->originalurl));
		$this->out->elementEnd('a');
	}
	
}

?>